<?php
//require_once("modules/CalendarCarrenet/calendar/carrenet/database.php");
require_once('include/JSON.php');

class AccountsController extends SugarController{

    public function action_loadUsersFromCities(){
        $json = getJSONobj();
        $request = array_merge($_POST, $_GET);
        //print_r($request);exit;
        $cityId = $request['cityId'];
        $term = $request['term'];

        $response = array();
        if(!empty($cityId)){
            $sql = "SELECT user_id, u.user_name
                FROM grp_cities_users gcu, users u
                WHERE gcu.user_id = u.id
                AND grp_city_id = '$cityId'
                AND (u.user_name LIKE '%$term%'
                    OR u.first_name LIKE '%$term%'
                    OR u.last_name LIKE '%$term%')";

            $result = $GLOBALS['db']->query($sql);
            while($row = $GLOBALS['db']->fetchByAssoc($result) ){
                $response[] = array('value' => $row['user_id'], 'label' => $row['user_name']);
            }

        }else{
            $response[] = array('value'=> '', 'label' => 'Wybierz najpierw miasto');
        }
        
        echo $json->encode($response);
    }
}